<?php
namespace app\html5\controller;
use app\Db2;

class Login{
    /**
     * @Notes: 患者端小程序登录
     * @Interface login
     * @author: Shaco
     * @Time: 2022/8/24-22:53
     */
    public function wxLogin(){
        $domain=request()->domain();
        $oauthurl = 'http://jlpa.miaosu8.cn/index.php/index/weixin/oauth2?backurl='.$domain.'/index.php/index/Settoken/setuser';
        header('Location: '.$oauthurl);//设置请求头跳转
        exit;
    }

    public function gettoken(){
        $id = input('id');

        //die(login_token($id));
        return rejson(200,['token'=>login_token($id)]);
    }

    public function setuser()
    {
        $openid = input('openid');
        $nickname = input('nickname');
        $headimgurl = input('headimgurl');
        $datalist = Db::table("think_user")->where(['openid' => $openid])->find();
        $res=0;
        if($datalist){
            $res=$datalist['id'];
        }else{
            $time = time();
            $code ='NPN'.time();
            $res = Db::table('think_user') -> insertGetId([
                'openid' => $openid,
                'nick_name' => $nickname,
                'headimg' => $headimgurl,
                'username' =>'',
                'password' =>'',
                'name' =>'',
                'type' =>0,
                'auth_type' =>0,
                'auth_approval' =>0,
                'user_code' =>$code,
                'end_login_time' =>$time
            ]);
        }

    }

    public function logout()
    {
        return rejson( 301,'token失效');
    }

}